/**************************************************************************
 *  创建者：        hualei
 *  开始时间：   2018.09.28
 *  copyright:  (C) 深圳市华友高科有限公司
 *  修改说明：  (每次有修改就添加一条，带有 修改人，修改时间，修改描述)
 *
 ***************************************************************************
 *   CHARGING 充电指令属性编辑视窗类
 *
 ***************************************************************************/

#include "charginginstructionview.h"
#include "ui_charginginstructionview.h"
#include "../../programView/programview.h"

ChargingInstructionView::ChargingInstructionView(int styleId,QWidget *parent) :
    QWidget(parent),
    ui(new Ui::ChargingInstructionView)
{
    ui->setupUi(this);
    initialDisplayStyle(styleId);

    // 充电时间选择项，增加小时
    for(int i = 0; i < 25; i++)
    {
        ui->Hour_ComBox->insertItem(i,QString::number(i));
    }
    // 充电时间选择项，增加分钟
    for(int i = 0; i < 61; i++)
    {
        ui->Minute_Combox->insertItem(i,QString::number(i));
    }

    // 充电容量滑块最大、最小值
    ui->ChargingCaparity_slider->setMaximum(100);
    ui->ChargingCaparity_slider->setMinimum(0);
    ui->ChargingCaparity_LineEdit->setValidator(new QIntValidator(0,100,this));

}

void ChargingInstructionView::initialDisplayStyle(int styleId)
{
    switch (styleId) {
    case 1:
        ui->forward_Btn->setGeometry(10,350,70,40);
        ui->save_Btn->setGeometry(360,350,70,40);
        break;
    default:
        break;
    }
}

void ChargingInstructionView::getInput()
{
    // 充电容量
    if(chargingFinishedType == E_CHARGING_CAPARITY)
    {
        chargingCaparity = ui->ChargingCaparity_slider->value();
    }
    // 充电时间
    else if(chargingFinishedType == E_CHARGING_TIME)
    {
        int hours = ui->Hour_ComBox->currentText().toInt();
        chargingTime = (hours*60) + ui->Minute_Combox->currentText().toInt();
    }
    // 取最先完成项
    else if(chargingFinishedType == E_CHARGING_FIRST_FINISHED)
    {
        chargingCaparity = ui->ChargingCaparity_slider->value();

        int hours = ui->Hour_ComBox->currentText().toInt();
        chargingTime = (hours*60) + ui->Minute_Combox->currentText().toInt();
    }

    noteStr = ui->note_lineEdit->text();
}

void ChargingInstructionView::updateView()
{
    ui->ChargingCaparity_RadioBtn->setChecked(false);
    ui->ChargingTime_RadioBtn->setChecked(false);
    ui->FirstFinish_RadioBtn->setChecked(false);

    ui->ChargingCaparity_LineEdit->setEnabled(false);
    ui->ChargingCaparity_slider->setEnabled(false);

    ui->Hour_ComBox->setEnabled(false);
    ui->Minute_Combox->setEnabled(false);

    if(chargingFinishedType == E_CHARGING_CAPARITY)
    {
        ui->ChargingCaparity_LineEdit->setEnabled(true);
        ui->ChargingCaparity_slider->setEnabled(true);
        ui->ChargingCaparity_RadioBtn->setChecked(true);
        ui->ChargingCaparity_slider->setValue(chargingCaparity);
    }
    else if(chargingFinishedType == E_CHARGING_TIME)
    {
        if(chargingTime >= 24*60)
        {
            ui->Hour_ComBox->setCurrentIndex(24);
            ui->Minute_Combox->setCurrentIndex(chargingTime - (24*60));
        }
        else
        {
            ui->Hour_ComBox->setCurrentIndex(chargingTime/60);
            ui->Minute_Combox->setCurrentIndex(chargingTime%60);
        }
        ui->ChargingTime_RadioBtn->setChecked(true);
        ui->Hour_ComBox->setEnabled(true);
        ui->Minute_Combox->setEnabled(true);
    }
    else if(chargingFinishedType == E_CHARGING_FIRST_FINISHED)
    {
        if(chargingTime >= 24*60)
        {
            ui->Hour_ComBox->setCurrentIndex(24);
            ui->Minute_Combox->setCurrentIndex(chargingTime - (24*60));
        }
        else
        {
            ui->Hour_ComBox->setCurrentIndex(chargingTime/60);
            ui->Minute_Combox->setCurrentIndex(chargingTime%60);
        }

        ui->ChargingCaparity_slider->setValue(chargingCaparity);
        ui->Hour_ComBox->setEnabled(true);
        ui->Minute_Combox->setEnabled(true);
        ui->ChargingCaparity_LineEdit->setEnabled(true);
        ui->ChargingCaparity_slider->setEnabled(true);
        ui->FirstFinish_RadioBtn->setChecked(true);
    }

    ui->note_lineEdit->setText(noteStr);
}

void ChargingInstructionView::changeLanguage(int language)
{
    if(1 == language)
    {
        translator.load("/agvwork/AgvHmi/baseBag/hmiProject/language/chargingInstructionView.qm");
        qApp->installTranslator(&translator);
    }
    else
    {
        qApp->removeTranslator(&translator);
    }
    ui->retranslateUi(this);

}

void ChargingInstructionView::on_clearNote_Btn_clicked()
{
    ui->note_lineEdit->text().clear();
    emit cleanNoteStrSignal();
}

void ChargingInstructionView::on_forward_Btn_clicked()
{
    this->close();
    programView->instructionListFlag = 0;
    programView->editInstructionViewShowOrHide();
}

void ChargingInstructionView::on_save_Btn_clicked()
{
    getInput();
    emit finishEditInstruction();
    emit saveInstructionSignal(instructionStr);
}

void ChargingInstructionView::on_ChargingTime_RadioBtn_clicked()
{
    chargingFinishedType = E_CHARGING_TIME;
    ui->Hour_ComBox->setEnabled(true);
    ui->Minute_Combox->setEnabled(true);
    ui->ChargingCaparity_LineEdit->setEnabled(false);
    ui->ChargingCaparity_slider->setEnabled(false);
}

void ChargingInstructionView::on_ChargingCaparity_RadioBtn_clicked()
{

    chargingFinishedType = E_CHARGING_CAPARITY;
    ui->Hour_ComBox->setEnabled(false);
    ui->Minute_Combox->setEnabled(false);
    ui->ChargingCaparity_LineEdit->setEnabled(true);
    ui->ChargingCaparity_slider->setEnabled(true);
}

void ChargingInstructionView::on_FirstFinish_RadioBtn_clicked()
{
    chargingFinishedType = E_CHARGING_FIRST_FINISHED;
    ui->Hour_ComBox->setEnabled(true);
    ui->Minute_Combox->setEnabled(true);
    ui->ChargingCaparity_LineEdit->setEnabled(true);
    ui->ChargingCaparity_slider->setEnabled(true);
}

void ChargingInstructionView::on_ChargingCaparity_slider_sliderMoved(int position)
{
    ui->ChargingCaparity_LineEdit->setText(QString::number(position));
}

void ChargingInstructionView::on_ChargingCaparity_LineEdit_textChanged(const QString &arg1)
{
    ui->ChargingCaparity_slider->setSliderPosition(arg1.toInt());
}

void ChargingInstructionView::on_ChargingCaparity_slider_valueChanged(int value)
{
    ui->ChargingCaparity_LineEdit->setText(QString::number(value));
}
